<?php

/* empty stubs */
function cable_entry_load($connector)
{
	return true;
}

function cable_entry_unload($connector)
{
	return true;
}

function cable_entry_open($connector)
{
	/* if material is cached, exit immediately */
	if ($connector->data[cached])
		return true;

	/* import and initialize plugin's global storage array */
	global $cable_entry_plugin;
	$cable_entry_plugin = array('fail'=>true);
		
	/* retrieve line template and remove from original */
	if (($cable_entry_plugin[line_template] = $connector->template->GetChild('CABLE-Line')) === false)
		return 'Error retrieving entry line template';
	$connector->template->ReplaceChild('CABLE-Line','');
		
	/* retrieve category template and remove from original */
	if (($cable_entry_plugin[category_template] = $connector->template->GetChild('CABLE-Category')) === false)
		return 'Error loading entry category template';
	$connector->template->ReplaceChild('CABLE-Category','');
	
	/* retrieve template value settings */
	$cable_entry_plugin[date_formats] = $connector->template->GetValue('CABLE-DateFormat',true);
	if (count($cable_entry_plugin[date_formats]) == 0)
		$cable_entry_plugin[date_formats] = array();
	
	$cable_entry_plugin[category_delimiter]	= (string) $connector->template->GetValue('CABLE-CategoryDelimiter');
	$cable_entry_plugin[array_delimiter]	= (string) $connector->template->GetValue('CABLE-ArrayDelimiter');
	
	/* passed initialization */
	$cable_entry_plugin[fail] = false;

	return true;
}

function cable_entry(&$connector)
{
	/* exit if material is cached already */
	if ($connector->data[cached])
		return true;

	global $cable_entry_plugin;
	
	/* sanity check */
	if ($cable_entry_plugin[fail])
		return true;

	/* negative timestamps are for preview, revert to positive for display */
	$connector->data[timestamp] = abs($connector->data[timestamp]);
	
	$line_counter = 1;
	foreach($connector->data[lines] as $entry_line)
	{
		$cable_entry_plugin[line_template]->ReplaceInsert('CABLE-Line.html',$entry_line);
		$cable_entry_plugin[line_template]->ReplaceInsert('CABLE-Line.num',$line_counter);
		
		/* save line template to our entry template */
		$connector->template->AppendInsert('CABLE-Lines',$cable_entry_plugin[line_template]->template);
		$cable_entry_plugin[line_template]->Reset();
		
		$line_counter++;
	}
	unset($cable_entry_plugin[line_template]);
	
	/* dynamically fill in serialized data */
	foreach($connector->data as $key => $value)
	{
		switch(gettype($value))
		{
			case 'boolean':
				$connector->template->SetBool('CABLE-'.ucfirst($key),$value);
				break;
			case 'array':
				$s = '';
				$counter = 0;
				foreach($connector->data[$key] as $k => $v)
				{
					if ($counter == 0)
						$s = '<a href="'.$connector->config[SiteURL]."search.php?m=$key&amp;s=$v\">$v</a>";
					else
						$s = $cable_entry_plugin[array_delimiter].'<a href="'.$connector->config[SiteURL]."search.php?m=$key&amp;s=$v\">$v</a>";
					$counter++;
				}
				$connector->template->ReplaceInsert('CABLE-'.ucfirst($key),$s);
				break;
			default:
				$connector->template->ReplaceInsert('CABLE-'.ucfirst($key),stripslashes(rawurldecode($value)));
				break;
		}
	}
	
	/* retrieve and replace author data */
	$user_table = "cable_".$connector->config['TablePrefix']."_users";
	$query = "SELECT username,fullname FROM $user_table WHERE (username = '".$connector->data['author']."') LIMIT 1";
	if ((($result = mysql_query($query,$connector->db_link)) !== false) && (mysql_num_rows($result) == 1))
	{
		$author_data = mysql_fetch_array($result, MYSQL_ASSOC);
		list($author_data['firstname'],$author_data['lastname']) = explode(' ',$author_data['fullname']);
		foreach($author_data as $f => $v)
			$connector->template->ReplaceInsert('CABLE-Author.'.$f,$v);
		$connector->template->ReplaceInsert('CABLE-Author',$author_data[username]);
	}
	else
		return 'Error accessing author database';
	
	/* retrieve category template and insert data as necessary */
	$counter = 0;
	foreach($connector->data[category] as $category)
	{
		$cable_entry_plugin[category_template]->ReplaceInsert('CABLE-Category.name',$category);
		/* insert the delimiter between categories */
		if ($counter == 0)
			$connector->template->AppendInsert('CABLE-Categories',$cable_entry_plugin[category_template]->template);
		else
			$connector->template->AppendInsert('CABLE-Categories',$cable_entry_plugin[category_delimiter].$cable_entry_plugin[category_template]->template);
		
		$cable_entry_plugin[category_template]->Reset();
		$counter++;
	}
	unset($cable_entry_plugin[category_template]);
		
	/* find entry previous to this one */
	$entry_table = "cable_".$connector->config['TablePrefix']."_entries";
	$query = "SELECT timestamp FROM $entry_table WHERE ((timestamp < '".$connector->timestamp."') AND (visible=1)) ORDER BY timestamp DESC LIMIT 1";
	if(($result = mysql_query($query,$connector->db_link)) !== false)
	{
		$connector->template->SetBool('CABLE-Previous',(mysql_num_rows($result) > 0));

		$row = mysql_fetch_row($result);
		if ($connector->config['EnableModRewrite'] > 0)
			$connector->template->ReplaceInsert('CABLE-Previous.url',$connector->config[SiteURL].$row[0]);
		else
			$connector->template->ReplaceInsert('CABLE-Previous.url',$connector->config[SiteURL].'entry.php?t='.$row[0]);	
	}
	else
		return 'Error accessing entry database.';
	
	/* find next entry */
	$query = "SELECT timestamp FROM $entry_table WHERE ((timestamp > '".$connector->timestamp."') AND (visible=1)) ORDER BY timestamp ASC LIMIT 1";
	if(($result = mysql_query($query,$connector->db_link)) !== false)
	{
		$connector->template->SetBool('CABLE-Next',(mysql_num_rows($result) > 0));
		
		$row = mysql_fetch_row($result);
		if ($connector->config['EnableModRewrite'] > 0)
			$connector->template->ReplaceInsert('CABLE-Next.url',$connector->config[SiteURL].$row[0]);
		else
			$connector->template->ReplaceInsert('CABLE-Next.url',$connector->config[SiteURL].'entry.php?t='.$row[0]);
	}
	else
		return 'Error accessing entry database.';
	
	return true;
}

function cable_entry_close($connector)
{
	global $cable_entry_plugin;

	/* sanity check */
	if ($cable_entry_plugin[fail])
		return true;

	/* STATIC TEMPLATE REPLACEMENTS */
	if (!$connector->data[cached])
	{	
		/* global replacement stuff */
		$connector->template->ReplaceInsert('CABLE-Site.name',$connector->config[SiteName]);
		$connector->template->ReplaceInsert('CABLE-Base.url',$connector->config[SiteURL]);
		$connector->template->ReplaceInsert('CABLE-Files.url',$connector->config[SiteURL].'files/'.$connector->config[ThemeName].'/');
		$connector->template->ReplaceInsert('CABLE-CSS.url',$connector->config[SiteURL].'css/'.$connector->config[ThemeName].'/');
	
		/* do standard date replacements */
		foreach($cable_entry_plugin[date_formats] as $i => $f)
			$connector->template->ReplaceInsert('CABLE-Date.'.$i,date($f,abs($connector->data[timestamp])));

		/* save non-specific information to the template original */
		$connector->template->Cache();
		
		/* free up plugin global */
		unset($cable_entry_plugin);
	}

	/* START DYNAMIC TEMPLATE REPLACEMENTS */
	/* set user info */
	$connector->template->SetBool('CABLE-User.loggedin',$connector->user->logged_in);
	$connector->template->SetBool('CABLE-User.isadmin',$connector->user->is_admin);
	$permissible = array('username','fullname','firstname','lastname');
	foreach($connector->user->values as $f => $v)
		if (in_array($f,$permissible))
			$connector->template->ReplaceInsert('CABLE-User.'.$f,$v);
			
	return true;
}

?>